var util = require('../../utils/util.js')
var items = ['播放列表', '歌曲', '专辑', '演唱者']
var _songsList = [{
  dataUrl: 'http://music.163.com/song/media/outer/url?id=470547655.mp3',
  name: 'byte',
  mid: "470547655",
  singer: 'Martin Garrix,Brooks',
  coverImgUrl: 'http://p4.music.126.net/4Wqe0O9eMEtB5ZDMbvwabQ==/19140298416382275.jpg?param=300x300'
}, {
  dataUrl: 'http://stream.qqmusic.tc.qq.com/138549169.mp3',
  name: '你还要我怎样',
  mid: "000E62Tc3bMiJB",
  singer: '薛之谦',
  coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R90x90M000000aWdOx24i3dG.jpg'
}, {
  dataUrl: 'http://stream.qqmusic.tc.qq.com/137903929.mp3',
  name: '微微一笑很倾城',
  mid: "002NbtFA3fuJhD",
  singer: '杨洋',
  coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R90x90M000003RxTdZ0sJLwo.jpg'
}, {
  dataUrl: 'http://stream.qqmusic.tc.qq.com/132636799.mp3',
  name: '演员',
  mid: "001Qu4I30eVFYb",
  singer: '薛之谦',
  coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R90x90M000003y8dsH2wBHlo.jpg'
}]
var _albumList = [{
  name: '寂寞不痛',
  singer: 'A-Lin',
  image: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000000Nlo922ahOEE.jpg?max_age=2592000'
}, {
  name: '喜剧之王',
  singer: '李荣浩',
  image: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000001FOctH2oGoAx.jpg?max_age=2592000'
}, {
  name: 'I Know You Were Trouble.-Single',
  singer: 'Taylor Swift',
  image: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000000XafSm26FA1L.jpg?max_age=2592000'
}, {
  name: '哎呦，不错哦',
  singer: '周杰伦',
  image: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000001uqejs3d6EID.jpg?max_age=2592000'
}]

var pageObject = {
  data: {
    playing: false,
    playingSongsNum: 0,
    musicGroupName: items[0],
    listTemplateName: 'music-play-list',
    actionSheetHidden: true,
    actionSheetItems: items,
    playBar: {
      dataUrl: 'http://music.163.com/song/media/outer/url?id=470547655.mp3',
      name: 'byte',
      singer: 'Martin Garrix,Brooks',
      coverImgUrl: 'http://p4.music.126.net/4Wqe0O9eMEtB5ZDMbvwabQ==/19140298416382275.jpg?param=300x300'
    },
    songsList: _songsList,
    albumList: _albumList
  },
  actionSheetTap: function (e) {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetChange: function (e) {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  onLoad: function () {

    // wx.request({
    //   url: "http://music.163.com/song/media/outer/url?id=470547655.mp3",
    //   method:'GET',
    //   success (res) {
    //     console.log(res)
    //   }
    // })
    // const backgroundAudioManager = wx.getBackgroundAudioManager()

  },
  play: function (event) {
    var res = this.data.songsList[event.currentTarget.dataset.num]
    this.setData({
      playBar: res,
      playingSongsNum: event.currentTarget.dataset.num
    })

    const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.src=res.dataUrl
    backgroundAudioManager.title=res.name
    backgroundAudioManager.singer=res.singer
    backgroundAudioManager.coverImgUrl= res.coverImgUrl
    backgroundAudioManager.playbackRate= 2
    backgroundAudioManager.play()
    this.setData({
      playing:true
    })
  },
  pause: function () {
    const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.pause()
    this.setData({
      playing:false
    })
  },
  onShow: function () {
    var that = this
    wx.request({
      url: 'http://localhost/song',
      data: {       
        id: 470547655	 
      },  
      dataType: 'json',
      success(res){
        that.setData({
          playBar: res.data
        })
      }
    })
  }
}

for (var i = 0; i < items.length; ++i) {
  (function (itemName) {
    switch (itemName) {
      case '播放列表':
        pageObject['bind' + itemName] = function (e) {
          console.log('click' + itemName, e)
          this.setData({
            musicGroupName: itemName,
            listTemplateName: 'music-play-list',
            templateData: null,
            actionSheetHidden: !this.data.actionSheetHidden
          })
        }
        break;

      case '歌曲':
        pageObject['bind' + itemName] = function (e) {
          console.log('click' + itemName, e)
          this.setData({
            musicGroupName: itemName,
            listTemplateName: 'songs-list',
            templateData: _songsList,
            actionSheetHidden: !this.data.actionSheetHidden
          })
        }
        break;

      case '专辑':
        pageObject['bind' + itemName] = function (e) {
          console.log('click' + itemName, e)
          this.setData({
            musicGroupName: itemName,
            listTemplateName: 'album-list',
            templateData: _albumList,
            actionSheetHidden: !this.data.actionSheetHidden
          })
        }
        break;

      case '演唱者':
        pageObject['bind' + itemName] = function (e) {
          console.log('click' + itemName, e)
          this.setData({
            musicGroupName: itemName,
            listTemplateName: 'singer-list',
            templateData: null,
            actionSheetHidden: !this.data.actionSheetHidden
          })
        }
        break;
    }

  })(items[i])
}



Page(pageObject)